package com.ddtech.business.course.controller;


import com.alibaba.fastjson.JSONObject;
import com.ddtech.annotation.AnonymousAccess;
import com.ddtech.business.course.entity.Course;
import com.ddtech.business.course.query.CourseQuery;
import com.ddtech.business.course.service.CourseService;
import com.ddtech.business.course.vo.CourseVo;
import com.ddtech.framework.log.aop.log.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author bin_pu
 * @since 2023-02-06
 */
@Api(tags = "业务-课程管理")
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "查询课程列表")
    @GetMapping(value = "/page-list")
    public ResponseEntity getPageList(CourseQuery courseQuery) {
        return new ResponseEntity(courseService.pageList(courseQuery), HttpStatus.OK);
    }

    @ApiOperation(value = "查询课程列表")
    @GetMapping(value = "/page-list-vr")
    public ResponseEntity getPageListVr(CourseQuery courseQuery) {
        return new ResponseEntity(courseService.pageListVr(courseQuery), HttpStatus.OK);
    }

    @ApiOperation(value = "查询课程列表")
    @GetMapping(value = "/page-course-list")
    public ResponseEntity getPageCourseList(CourseQuery courseQuery) {
        return new ResponseEntity(courseService.pageCourseList(courseQuery), HttpStatus.OK);
    }

    @ApiOperation(value = "查询推荐、热门课程列表-首页PC、移动端")
    @GetMapping(value = "/page-course-list-pc")
    @AnonymousAccess
    public ResponseEntity getPageCourseListPc(CourseQuery courseQuery) {
        return new ResponseEntity(courseService.pageCourseListPc(courseQuery), HttpStatus.OK);
    }

    @ApiOperation(value = "查询分类课程列表-首页PC、移动端")
    @GetMapping(value = "/page-course-list-type")
    public ResponseEntity getPageCourseListType(CourseQuery courseQuery) {
        return new ResponseEntity(courseService.pageCourseListType(courseQuery), HttpStatus.OK);
    }

    @ApiOperation(value = "课程搜索、课程列表-首页PC、移动端")
    @GetMapping(value = "/page-course-list-search")
    @AnonymousAccess
    public ResponseEntity getPageCourseListSearch(CourseQuery courseQuery) {
        return new ResponseEntity(courseService.pageCourseListSearch(courseQuery), HttpStatus.OK);
    }

    @Log("新增课程")
    @ApiOperation(value = "新增课程")
    @PostMapping(value = "/create")
    public ResponseEntity create(@RequestBody CourseVo courseVo) {
        courseService.create(courseVo);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("新增课程-vr")
    @ApiOperation(value = "新增课程-vr")
    @PostMapping(value = "/createVr")
    public ResponseEntity create(@RequestBody Course course) {
        return new ResponseEntity(courseService.createVr(course),HttpStatus.OK);
    }

    @ApiOperation(value = "课程详情")
    @GetMapping(value = "/info/{id}")
    public ResponseEntity info(@PathVariable String id) {
        return new ResponseEntity(courseService.info(id), HttpStatus.OK);
    }


    @ApiOperation(value = "PC端、移动端课程详情")
    @GetMapping(value = "/getCourseInfo/{id}")
    @AnonymousAccess
    public ResponseEntity getCourseInfo(@PathVariable String id) {
        return new ResponseEntity(courseService.getCourseInfo(id), HttpStatus.OK);
    }

    @ApiOperation(value = "PC端、移动端获取课程章节、目录")
    @GetMapping(value = "/getCourseChapter/{id}")
    @AnonymousAccess
    public ResponseEntity getCourseChapter(@PathVariable String id) {
        return new ResponseEntity(courseService.getCourseChapter(id), HttpStatus.OK);
    }

    @Log("删除课程")
    @ApiOperation(value = "删除课程分类")
    @PostMapping(value = "/del/{id}")
    public ResponseEntity del(@PathVariable String id) {
        courseService.del(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("删除课程-vr")
    @ApiOperation(value = "删除课程-vr")
    @PostMapping(value = "/delVr/{id}")
    public ResponseEntity delVr(@PathVariable String id) {
        courseService.removeById(id);
        return new ResponseEntity(true,HttpStatus.OK);
    }

    @Log("删除课程类型")
    @ApiOperation(value = "删除课程类型")
    @PostMapping(value = "/delType/{id}")
    public ResponseEntity delType(@PathVariable String id) {
        courseService.delType(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @ApiOperation(value = "课程详情")
    @GetMapping(value = "/findOne/{id}")
    public ResponseEntity findOne(@PathVariable String id) {
        Course course = courseService.getById(id);
        if (course!=null){
            JSONObject json = new JSONObject();
            json.put("id",course.getId());
            json.put("courseName",course.getCourseName());
            json.put("themeImage",course.getThemeImage());
            json.put("source",course.getThemeImage());
            json.put("courseType",course.getCourseType());
            json.put("courseDesc",course.getCourseDesc());
            return new ResponseEntity(json,HttpStatus.OK);
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}
